<template>
  <div class="modifypassword">
    <van-nav-bar
      title="修改密码"
      left-text="上一页"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="oldpassword"
        type="password"
        name="oldPassword"
        label="旧密码"
        placeholder="请输入旧密码"
        :rules="[{ required: true, message: '请填写旧密码' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <van-field
        v-model="repassword"
        type="password"
        name="password"
        label="密码"
        placeholder="请再次输入密码密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" size="large" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { passModi } from "../services/user.js";
import { removeToken } from "../utils/tools";
import { Toast } from "vant";
export default {
  name: "ModiPwd",
  data() {
    return {
      oldpassword: "",
      password: "",
      repassword: "",
    };
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1);
    },
    // 完成修改保存
    onSubmit(values) {
      if (this.oldpassword && this.repassword === this.password) {
        passModi(values).then((res) => {
          this.oldpassword = "";
          this.password = "";
          this.repassword = "";
          // 如果修改成功，就弹出提示信息，并且删除token 重新登陆
          if (res.code === 1) {
            removeToken();
            Toast.success(res.msg);
            this.$router.push({
              name: "Login",
            });
          } else {
            Toast.fail("旧密码输入错误");
          }
        });
      } else {
        Toast.fail("两次密码不一致");
      }
    },
  },
};
</script>

<style></style>
